//
//  profileViewController.m
//  giuseppe
//
//  Created by Apple on 15/7/13.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "profileViewController.h"
#import "define.h"
#import "changeUserViewController.h"
#import "myDataAccess.h"
#import "profileInfoModel.h"

//push页面
#import "messageViewController.h"
#import "profileViewController.h"
#import "shoppingCartViewController.h"
#import "productDetailsViewController.h"
#import "favoritesViewController.h"
#import "orderViewController.h"
#import "notReceivedViewController.h"
#import "receivedViewController.h"
#import "messageViewController.h"
#import "settingViewController.h"
#import "shareViewController.h"
#import "homeViewController.h"



@interface profileViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSMutableArray *_listArray;
    NSArray *_contentsArray;
    UIView *coverView;
    UITableView *leftTableView;
    CGPoint firstPoint;
    CGPoint lastPoint;
    NSArray *array;//左侧文字数组
    NSMutableArray *imageArray;//中央图片数组

}

@end

@implementation profileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.navigationController.navigationBarHidden = YES;
    
    _listArray = [NSMutableArray arrayWithObjects:@"性别",@"身高",@"体重",@"出色年月",@"学校",@"班级",nil];

    imageArray = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < 4; i ++) {
        NSString *str = [NSString stringWithFormat:@"profileImage%d",i+1];
        UIImage *image = [UIImage imageNamed:str];
        [imageArray addObject:image];
    }
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;

    
    //左侧个人中心按钮
    UIButton *profileBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [profileBtn setFrame:CGRectMake(0, 20, 50, 50)];
    [profileBtn setImage:[UIImage imageNamed:@"profileButton"] forState:UIControlStateNormal];
    [profileBtn addTarget:self action:@selector(profile) forControlEvents:UIControlEventTouchUpInside];

    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"个人信息";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIButton *changeUserButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeUserButton setTitle:@"切换账号" forState:UIControlStateNormal];
    changeUserButton.frame = CGRectMake(kScreenWidth-80, 20, 80, 40);
    [changeUserButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    changeUserButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [changeUserButton addTarget:self action:@selector(changeUser) forControlEvents:UIControlEventTouchUpInside];
    
    
    //遮罩
    coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.00;

    
    
    [self loadData];
    [self _initTableView];
    
    
//    [navView addSubview:backButton];
    [navView addSubview:profileBtn];
    [navView addSubview:titleLabel];
    [navView addSubview:changeUserButton];
    
    
    [self.view addSubview:navView];
    
    [self.view addSubview:coverView];

    [self initLifeTableView];
    
    //点击主页收回事件
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(event:)];
    [coverView addGestureRecognizer:tapGesture];

    
}

- (void)loadData{
    
//    //请求参数
//    NSDictionary *paras= @{@"userId":@"dsadas",
//                           @"userIdentityNumber":@"dsadasd",
//                           @"type":@"0"};
//    //调用请求0
//    [[myDataAccess shardWebUtil] userInfoManagementWithURLString:@"/api/userInfoManagement"  andParas:paras andTransferGottenData:^(id obj) {
//        
//        NSLog(@"%@",obj);
//
//
//        profileInfoModel *model = [[profileInfoModel alloc] initJsonModelWithDictionary:obj];
//        [_contentsArray addObject:model.birthday];
//   
//   
//    }];
//
    
    NSDictionary *obj =@{@"resultCode":@"dsadas",@"imgurl":@"dsadasd",@"gender":@"dsad",@"weight":@"0",@"birthday":@"0",@"school":@"0",@"class":@"0",@"userIdentityNumber":@"0",@"name":@"0"};

    profileInfoModel *model = [profileInfoModel initWithDicAttributes:obj];
    _contentsArray = @[model.gender,model.weight,model.birthday,model.school,model.name,model.userIdentityNumber];
    

    
}


- (void)initLifeTableView{
    
    leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(-kScreenWidth/5*3, 0, kScreenWidth/5*3, [UIScreen mainScreen].bounds.size.height)];
    
    leftTableView.delegate = self;
    leftTableView.dataSource = self;
    leftTableView.tag = 102;
    
    //tableView头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/5*3, 120)];
    headerView.backgroundColor = baseColor;
    
    UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 40, 50, 50)];
    headerImage.image = [UIImage imageNamed:@"cover6"];
    headerImage.layer.masksToBounds = YES;
    headerImage.layer.cornerRadius = 25;
    
    
    UILabel *schoolLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 40, 100, 30)];
    schoolLabel.text = @"广场路小学";
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 70, 100, 30)];
    nameLabel.text = @"张小光";
    
    
    [headerView addSubview:headerImage];
    [headerView addSubview:schoolLabel];
    [headerView addSubview:nameLabel];
    
    leftTableView.tableHeaderView = headerView;
    
    
    //tableView尾视图
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/5*3, 50)];
    footerView.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *logoutButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 30, 60, 20)];
    [logoutButton setTitle:@"退出" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [footerView addSubview:logoutButton];
    
    
    leftTableView.tableFooterView = footerView;
    
    
    
    [self.view addSubview:leftTableView];
}



- (void)_initTableView{
    
    _tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _tableView.tag = 101;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = mainColor;
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/3)];
    headerView.backgroundColor = mainColor;
    
    UIImageView *userImage = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2-30, 80, 60, 60)];
    userImage.backgroundColor = baseColor;
    userImage.layer.masksToBounds = YES;
    userImage.layer.cornerRadius = 30;
    
    [headerView addSubview:userImage];
    
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-30, 145, 60, 20)];
    userNameLabel.text = @"小明";
    userNameLabel.textAlignment = NSTextAlignmentCenter;
    
    [headerView addSubview:userNameLabel];
    
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    
    footerView.backgroundColor = [UIColor whiteColor];

    _tableView.tableHeaderView = headerView;
    _tableView.tableFooterView = footerView;
    [self.view addSubview:_tableView];
    
    
}

#pragma mark - Table view data source && delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 101) {
        return 10;
    }else{
        
    return 7;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 101) {
    
        static NSString *indentify = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentify];
        
        if (indexPath.row == 0||indexPath.row == 1||indexPath.row == 2||indexPath.row == 3||indexPath.row == 4||indexPath.row == 5){
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 100, 25)];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = _listArray[indexPath.row];
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-100, 10, 60, 25)];
        contentLabel.textAlignment = NSTextAlignmentRight;
        contentLabel.text = _contentsArray[indexPath.row];

    
        [cell.contentView addSubview:titleLabel];
        [cell.contentView addSubview:contentLabel];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        if (indexPath.row == 6||indexPath.row == 7||indexPath.row == 8||indexPath.row == 9){
            
            UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 132)];
            if (indexPath.row == 6) {
                backgroundImageView.frame = CGRectMake(0, 0, kScreenWidth, 160);
            }
            
            backgroundImageView.image = imageArray[indexPath.row-6];
 
            NSArray *nameLabelArray = [[NSArray alloc]init];
            nameLabelArray = @[@"我的校服",@"我的课程表",@"我的手表",@"我的睡眠"];
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, kScreenWidth, 30)];
            nameLabel.text = nameLabelArray[indexPath.row-6];
            nameLabel.textColor = [UIColor whiteColor];
            nameLabel.textAlignment = NSTextAlignmentCenter;
            
            [cell.contentView addSubview:backgroundImageView];
            [cell.contentView addSubview:nameLabel];
        }
 
        return cell;

    }else{
        
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        
        
        array = @[@"收藏夹",@"未收货",@"已收货",@"全部订单",@"邀请",@"设置",@"消息"];
        NSString *labelText = array[indexPath.row];
        cell.textLabel.text = labelText;
        
        if (indexPath.row == 6) {
            UIImageView *redPointImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 8, 8)];
            redPointImageView.backgroundColor = [UIColor redColor];
            redPointImageView.layer.masksToBounds = YES;
            redPointImageView.layer.cornerRadius = 4;
            [cell.contentView addSubview:redPointImageView];
        }
        
        return cell;
        
    }

}



//设置点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (tableView.tag == 102) {
        if(indexPath.row == 0){
            favoritesViewController *favoritesVC = [[favoritesViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:favoritesVC animated:YES];
        }if(indexPath.row == 1){
            notReceivedViewController *notReceivedVC = [[notReceivedViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:notReceivedVC animated:YES];
        }if (indexPath.row == 2) {
            receivedViewController *receivedVC = [[receivedViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:receivedVC animated:YES];
        }if (indexPath.row == 3) {
            orderViewController *orderVC = [[orderViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:orderVC animated:YES];
        }if (indexPath.row == 4) {
            shareViewController *shareVC = [[shareViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:shareVC animated:YES];
        }if (indexPath.row == 5) {
            settingViewController *settingVC = [[settingViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:settingVC animated:YES];
        }if (indexPath.row == 6) {
            messageViewController *messageVC = [[messageViewController alloc]init];
            [self hideView];
            [self.navigationController pushViewController:messageVC animated:YES];
        }
        
    }else{
        if (indexPath.row == 6) {
            homeViewController *homeVC = [[homeViewController alloc]init];
            [self.navigationController pushViewController:homeVC animated:YES];
        }
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 101) {
        if (indexPath.row == 0||indexPath.row == 1||indexPath.row == 2||indexPath.row == 3||indexPath.row == 4||indexPath.row == 5 ) {
            return 50;
        }
    else{
        if (indexPath.row == 6) {
            return 160;
        }else{
            return 130;
        }
    }
    }else{
        return 50;
    }
}



#pragma mark PanGesture
- (void)event:(UITapGestureRecognizer *)gesture{
    
    [self hideView];
    
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    
    //1.触摸开始
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        firstPoint = [pan locationInView:self.view];
        
    }
    //2.触摸移动
    else if(pan.state == UIGestureRecognizerStateChanged) {
        
        lastPoint = [pan locationInView:self.view];
        
        
        leftTableView.frame = CGRectMake(0+(lastPoint.x-firstPoint.x), 0, kScreenWidth/5*3, [UIScreen mainScreen].bounds.size.height);
        
        
    }
    //3.结束触摸
    else if(pan.state == UIGestureRecognizerStateEnded) {
        
        if ((firstPoint.x-lastPoint.x)>100) {
            
            [self hideView];
            
        }
        else{
            
            [self showView];
            
        }
    }
    
}


- (void)showView{
    
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
        leftTableView.frame = CGRectMake(0, 0, kScreenWidth/5*3, [UIScreen mainScreen].bounds.size.height);
        coverView.alpha = 0.50;
        
    }completion:^(BOOL finish){
        
        NSLog(@"移出成功");
        
    }];
    
}

- (void)hideView{
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
        leftTableView.frame = CGRectMake(-kScreenWidth/5*3, 0, kScreenWidth/5*3, [UIScreen mainScreen].bounds.size.height);
        
        coverView.alpha = 0.00;
        
    }completion:^(BOOL finish){
        
        NSLog(@"移除成功");
        
    }];
    
}


- (void)profile{
    
    [self showView];
    
}


- (void)changeUser{
    
    changeUserViewController *changeUserVC = [[changeUserViewController alloc]init];
    [self.navigationController pushViewController:changeUserVC animated:YES];
    
}


- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
